<template>
  <div class="page-modle">
    <el-dialog
      v-model="centerDialogVisible"
      :title="Modalconfig.handleUser"
      width="30%"
      center
      destroy-on-close
    >
      <div class="modal-cont">
        <LetForm v-bind="Modalconfig" v-model="formData"></LetForm>
      </div>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleConfirm">确认</el-button>
        </span>
      </template>
      <slot></slot>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, watch } from "vue";
import LetForm from "@/base-ui/from";
import store from "@/store";

export default defineComponent({
  props: {
    Modalconfig: {
      type: Object,
      default: () => ({}),
    },
    defaultInfo: {
      type: Object,
      default: () => ({}),
    },
    pageName: {
      type: String,
    },
  },
  components: {
    LetForm,
  },
  setup(props) {
    const centerDialogVisible = ref(false);
    const formData = ref<any>({});

    watch(
      () => props.defaultInfo,
      (newValue) => {
        for (const item of props.Modalconfig.fromItems) {
          formData.value[`${item.field}`] = newValue[`${item.field}`];
        }
      }
    );

    // 确认和取消
    const handleConfirm = () => {
      //Object.keys() 方法会返回一个由一个给定对象的自身可枚举属性组成的数组，数组中属性名的排列顺序和正常循环遍历该对象时返回的顺序一致 。
      centerDialogVisible.value = false;
      if (Object.keys(props.defaultInfo).length) {
        //编辑
        store.dispatch("system/editPageDataActions", {
          pageName: props.pageName,
          editData: { ...formData.value },
          id: props.defaultInfo.id,
        });
      } else {
        //新建
        store.dispatch("system/createPageDataActions", {
          pageName: props.pageName,
          createData: { ...formData.value },
        });
      }
    };

    return { LetForm, centerDialogVisible, formData, handleConfirm };
  },
});
</script>

<style scoped lang="less"></style>
